// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.sadisticAIDirector;

import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.characters.BodyDamage.BodyPart;
import zombie.characters.BodyDamage.BodyPartType;
import zombie.characters.Moodles.MoodleType;
import zombie.core.Rand;
import zombie.core.logger.ExceptionLogger;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;
import zombie.iso.objects.IsoBarricade;
import zombie.iso.objects.IsoDoor;
import zombie.iso.objects.IsoLightSwitch;
import zombie.iso.objects.IsoRadio;
import zombie.iso.objects.IsoStove;
import zombie.iso.objects.IsoTelevision;
import zombie.iso.objects.IsoWindow;
import zombie.iso.weather.ClimateManager;
import zombie.network.GameClient;
import zombie.ui.UIManager;
import zombie.util.Type;
import zombie.vehicles.BaseVehicle;

public final class SleepingEvent {
    public static final SleepingEvent instance = new SleepingEvent();
    public static boolean zombiesInvasion = false;

    public void setPlayerFallAsleep(IsoPlayer chr, int sleepingTime) {
        SleepingEventData sleepingEventData = chr.getOrCreateSleepingEventData();
        sleepingEventData.reset();
        if (!IsoWorld.getZombiesEnabled()) {
            if (ClimateManager.getInstance().isRaining() && this.isExposedToPrecipitation(chr)) {
                sleepingEventData.bRaining = true;
                sleepingEventData.bWasRainingAtStart = true;
                sleepingEventData.rainTimeStartHours = zombie.GameTime.getInstance().getWorldAgeHours();
            }

            sleepingEventData.sleepingTime = (float)sleepingTime;
            chr.setTimeOfSleep(zombie.GameTime.instance.getTimeOfDay());
            this.doDelayToSleep(chr);
            this.checkNightmare(chr, sleepingTime);
            if (sleepingEventData.nightmareWakeUp <= -1) {
                if (zombie.SandboxOptions.instance.SleepingEvent.getValue() != 1 && zombiesInvasion) {
                    if (chr.getCurrentSquare() == null || chr.getCurrentSquare().getZone() == null || !chr.getCurrentSquare().getZone().haveConstruction) {
                        boolean boolean0 = false;
                        if ((zombie.GameTime.instance.getHour() >= 0 && zombie.GameTime.instance.getHour() < 5 || zombie.GameTime.instance.getHour() > 18)
                            && sleepingTime >= 4) {
                            boolean0 = true;
                        }

                        byte _byte = 20;
                        if (zombie.SandboxOptions.instance.SleepingEvent.getValue() == 3) {
                            _byte = 45;
                        }

                        if (Rand.Next(100) <= _byte && chr.getCell().getZombieList().size() >= 1 && sleepingTime >= 4) {
                            int int0 = 0;
                            if (chr.getCurrentBuilding() != null) {
                                IsoGridSquare isoGridSquare = null;
                                Object object = null;

                                for (int int1 = 0; int1 < 3; int1++) {
                                    for (int int2 = chr.getCurrentBuilding().getDef().getX() - 2; int2 < chr.getCurrentBuilding().getDef().getX2() + 2; int2++) {
                                        for (int int3 = chr.getCurrentBuilding().getDef().getY() - 2;
                                            int3 < chr.getCurrentBuilding().getDef().getY2() + 2;
                                            int3++
                                        ) {
                                            isoGridSquare = IsoWorld.instance.getCell().getGridSquare(int2, int3, int1);
                                            if (isoGridSquare != null) {
                                                boolean boolean1 = isoGridSquare.haveElectricity() || IsoWorld.instance.isHydroPowerOn();
                                                if (boolean1) {
                                                    for (int int4 = 0; int4 < isoGridSquare.getObjects().size(); int4++) {
                                                        IsoObject isoObject = isoGridSquare.getObjects().get(int4);
                                                        if (isoObject.getContainer() != null
                                                            && (
                                                                isoObject.getContainer().getType().equals("fridge")
                                                                    || isoObject.getContainer().getType().equals("freezer")
                                                            )) {
                                                            int0 += 3;
                                                        }

                                                        if (isoObject instanceof IsoStove && ((IsoStove)isoObject).Activated()) {
                                                            int0 += 5;
                                                        }

                                                        if (isoObject instanceof IsoTelevision && ((IsoTelevision)isoObject).getDeviceData().getIsTurnedOn()) {
                                                            int0 += 30;
                                                        }

                                                        if (isoObject instanceof IsoRadio && ((IsoRadio)isoObject).getDeviceData().getIsTurnedOn()) {
                                                            int0 += 30;
                                                        }
                                                    }
                                                }

                                                object = isoGridSquare.getWindow();
                                                if (object != null) {
                                                    int0 += this.checkWindowStatus((IsoWindow)object);
                                                }

                                                IsoDoor isoDoor = isoGridSquare.getIsoDoor();
                                                if (isoDoor != null && isoDoor.isExteriorDoor(null) && isoDoor.IsOpen()) {
                                                    int0 += 25;
                                                    sleepingEventData.openDoor = isoDoor;
                                                }
                                            }
                                        }
                                    }
                                }

                                if (zombie.SandboxOptions.instance.SleepingEvent.getValue() == 3) {
                                    int0 = (int)((double)int0 * 1.5);
                                }

                                if (int0 > 70) {
                                    int0 = 70;
                                }

                                if (!boolean0) {
                                    int0 /= 2;
                                }

                                if (Rand.Next(100) <= int0) {
                                    sleepingEventData.forceWakeUpTime = Rand.Next(sleepingTime - 4, sleepingTime - 1);
                                    sleepingEventData.zombiesIntruders = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private void doDelayToSleep(IsoPlayer isoPlayer) {
        float float0 = 0.3F;
        float float1 = 2.0F;
        if (isoPlayer.Traits.Insomniac.isSet()) {
            float0 = 1.0F;
        }

        if (isoPlayer.getMoodles().getMoodleLevel(MoodleType.Pain) > 0) {
            float0 += 1.0F + (float)isoPlayer.getMoodles().getMoodleLevel(MoodleType.Pain) * 0.2F;
        }

        if (isoPlayer.getMoodles().getMoodleLevel(MoodleType.Stress) > 0) {
            float0 *= 1.2F;
        }

        if ("badBed".equals(isoPlayer.getBedType())) {
            float0 *= 1.3F;
        } else if ("goodBed".equals(isoPlayer.getBedType())) {
            float0 *= 0.8F;
        } else if ("floor".equals(isoPlayer.getBedType())) {
            float0 *= 1.6F;
        }

        if (isoPlayer.Traits.NightOwl.isSet()) {
            float0 *= 0.5F;
        }

        if (isoPlayer.getSleepingTabletEffect() > 1000.0F) {
            float0 = 0.1F;
        }

        if (float0 > float1) {
            float0 = float1;
        }

        float float2 = Rand.Next(0.0F, float0);
        isoPlayer.setDelayToSleep(zombie.GameTime.instance.getTimeOfDay() + float2);
    }

    private void checkNightmare(IsoPlayer isoPlayer, int int0) {
        if (!GameClient.bClient) {
            SleepingEventData sleepingEventData = isoPlayer.getOrCreateSleepingEventData();
            if (int0 >= 3) {
                int int1 = 5 + isoPlayer.getMoodles().getMoodleLevel(MoodleType.Stress) * 10;
                if (Rand.Next(100) < int1) {
                    sleepingEventData.nightmareWakeUp = Rand.Next(3, int0 - 2);
                }
            }
        }
    }

    private int checkWindowStatus(IsoWindow isoWindow) {
        IsoGridSquare isoGridSquare = isoWindow.getSquare();
        if (isoWindow.getSquare().getRoom() == null) {
            if (!isoWindow.north) {
                isoGridSquare = isoWindow.getSquare()
                    .getCell()
                    .getGridSquare(isoWindow.getSquare().getX() - 1, isoWindow.getSquare().getY(), isoWindow.getSquare().getZ());
            } else {
                isoGridSquare = isoWindow.getSquare()
                    .getCell()
                    .getGridSquare(isoWindow.getSquare().getX(), isoWindow.getSquare().getY() - 1, isoWindow.getSquare().getZ());
            }
        }

        byte _byte = 0;
        boolean _boolean = false;

        for (int _int = 0; _int < isoGridSquare.getRoom().lightSwitches.size(); _int++) {
            if (((IsoLightSwitch)isoGridSquare.getRoom().lightSwitches.get(_int)).isActivated()) {
                _boolean = true;
                break;
            }
        }

        if (_boolean) {
            _byte = 20;
            if (isoWindow.HasCurtains() != null && !isoWindow.HasCurtains().open) {
                _byte -= 17;
            }

            IsoBarricade isoBarricade0 = isoWindow.getBarricadeOnOppositeSquare();
            if (isoBarricade0 == null) {
                isoBarricade0 = isoWindow.getBarricadeOnSameSquare();
            }

            if (isoBarricade0 != null && (isoBarricade0.getNumPlanks() > 4 || isoBarricade0.isMetal())) {
                _byte -= 20;
            }

            if (_byte < 0) {
                _byte = 0;
            }

            if (isoGridSquare.getZ() > 0) {
                _byte /= 2;
            }

            return _byte;
        } else {
            _byte = 5;
            if (isoWindow.HasCurtains() != null && !isoWindow.HasCurtains().open) {
                _byte -= 5;
            }

            IsoBarricade isoBarricade1 = isoWindow.getBarricadeOnOppositeSquare();
            if (isoBarricade1 == null) {
                isoBarricade1 = isoWindow.getBarricadeOnSameSquare();
            }

            if (isoBarricade1 != null && (isoBarricade1.getNumPlanks() > 3 || isoBarricade1.isMetal())) {
                _byte -= 5;
            }

            if (_byte < 0) {
                _byte = 0;
            }

            if (isoGridSquare.getZ() > 0) {
                _byte /= 2;
            }

            return _byte;
        }
    }

    public void update(IsoPlayer chr) {
        if (chr != null) {
            SleepingEventData sleepingEventData = chr.getOrCreateSleepingEventData();
            if (sleepingEventData.nightmareWakeUp == (int)chr.getAsleepTime()) {
                chr.getStats().Panic += 70.0F;
                chr.getStats().stress += 0.5F;
                zombie.WorldSoundManager.instance.addSound(chr, (int)chr.getX(), (int)chr.getY(), (int)chr.getZ(), 6, 1);
                zombie.SoundManager.instance.setMusicWakeState(chr, "WakeNightmare");
                this.wakeUp(chr);
            }

            if (sleepingEventData.forceWakeUpTime == (int)chr.getAsleepTime() && sleepingEventData.zombiesIntruders) {
                this.spawnZombieIntruders(chr);
                zombie.WorldSoundManager.instance.addSound(chr, (int)chr.getX(), (int)chr.getY(), (int)chr.getZ(), 6, 1);
                zombie.SoundManager.instance.setMusicWakeState(chr, "WakeZombies");
                this.wakeUp(chr);
            }

            this.updateRain(chr);
            this.updateSnow(chr);
            this.updateTemperature(chr);
            this.updateWetness(chr);
        }
    }

    private void updateRain(IsoPlayer isoPlayer) {
        SleepingEventData sleepingEventData = isoPlayer.getOrCreateSleepingEventData();
        if (!ClimateManager.getInstance().isRaining()) {
            sleepingEventData.bRaining = false;
            sleepingEventData.bWasRainingAtStart = false;
            sleepingEventData.rainTimeStartHours = -1.0;
        } else if (this.isExposedToPrecipitation(isoPlayer)) {
            double _double = zombie.GameTime.getInstance().getWorldAgeHours();
            if (!sleepingEventData.bWasRainingAtStart) {
                if (!sleepingEventData.bRaining) {
                    sleepingEventData.rainTimeStartHours = _double;
                }

                if (sleepingEventData.getHoursSinceRainStarted() >= 0.16666666666666666) {
                }
            }

            sleepingEventData.bRaining = true;
        }
    }

    private void updateSnow(IsoPlayer isoPlayer) {
        if (ClimateManager.getInstance().isSnowing()) {
            if (this.isExposedToPrecipitation(isoPlayer)) {
                ;
            }
        }
    }

    private void updateTemperature(IsoPlayer var1) {
    }

    private void updateWetness(IsoPlayer var1) {
    }

    private boolean isExposedToPrecipitation(IsoGameCharacter isoGameCharacter) {
        if (isoGameCharacter.getCurrentSquare() == null) {
            return false;
        } else if (isoGameCharacter.getCurrentSquare().isInARoom() || isoGameCharacter.getCurrentSquare().haveRoof) {
            return false;
        } else if (isoGameCharacter.getBed() != null && "Tent".equals(isoGameCharacter.getBed().getName())) {
            return false;
        } else {
            BaseVehicle baseVehicle = isoGameCharacter.getVehicle();
            return baseVehicle == null || !baseVehicle.hasRoof(baseVehicle.getSeat(isoGameCharacter));
        }
    }

    private void spawnZombieIntruders(IsoPlayer isoPlayer) {
        SleepingEventData sleepingEventData = isoPlayer.getOrCreateSleepingEventData();
        IsoGridSquare isoGridSquare = null;
        if (sleepingEventData.openDoor != null) {
            isoGridSquare = sleepingEventData.openDoor.getSquare();
        } else {
            sleepingEventData.weakestWindow = this.getWeakestWindow(isoPlayer);
            if (sleepingEventData.weakestWindow != null && sleepingEventData.weakestWindow.getZ() == 0.0F) {
                if (!sleepingEventData.weakestWindow.north) {
                    if (sleepingEventData.weakestWindow.getSquare().getRoom() == null) {
                        isoGridSquare = sleepingEventData.weakestWindow.getSquare();
                    } else {
                        isoGridSquare = sleepingEventData.weakestWindow
                            .getSquare()
                            .getCell()
                            .getGridSquare(
                                sleepingEventData.weakestWindow.getSquare().getX() - 1,
                                sleepingEventData.weakestWindow.getSquare().getY(),
                                sleepingEventData.weakestWindow.getSquare().getZ()
                            );
                    }
                } else if (sleepingEventData.weakestWindow.getSquare().getRoom() == null) {
                    isoGridSquare = sleepingEventData.weakestWindow.getSquare();
                } else {
                    isoGridSquare = sleepingEventData.weakestWindow
                        .getSquare()
                        .getCell()
                        .getGridSquare(
                            sleepingEventData.weakestWindow.getSquare().getX(),
                            sleepingEventData.weakestWindow.getSquare().getY() + 1,
                            sleepingEventData.weakestWindow.getSquare().getZ()
                        );
                }

                IsoBarricade isoBarricade = sleepingEventData.weakestWindow.getBarricadeOnOppositeSquare();
                if (isoBarricade == null) {
                    isoBarricade = sleepingEventData.weakestWindow.getBarricadeOnSameSquare();
                }

                if (isoBarricade != null) {
                    isoBarricade.Damage(Rand.Next(500, 900));
                } else {
                    sleepingEventData.weakestWindow.Damage(200.0F);
                    sleepingEventData.weakestWindow.smashWindow();
                    if (sleepingEventData.weakestWindow.HasCurtains() != null) {
                        sleepingEventData.weakestWindow.removeSheet(null);
                    }

                    if (isoGridSquare != null) {
                        isoGridSquare.addBrokenGlass();
                    }
                }
            }
        }

        isoPlayer.getStats().setPanic(isoPlayer.getStats().getPanic() + (float)Rand.Next(30, 60));
        if (isoGridSquare != null) {
            if (IsoWorld.getZombiesEnabled()) {
                int int0 = Rand.Next(3) + 1;

                for (int int1 = 0; int1 < int0; int1++) {
                    zombie.VirtualZombieManager.instance.choices.clear();
                    zombie.VirtualZombieManager.instance.choices.add(isoGridSquare);
                    IsoZombie isoZombie = zombie.VirtualZombieManager.instance.createRealZombieAlways(IsoDirections.fromIndex(Rand.Next(8)).index(), false);
                    if (isoZombie != null) {
                        isoZombie.setTarget(isoPlayer);
                        isoZombie.pathToCharacter(isoPlayer);
                        isoZombie.spotted(isoPlayer, true);
                        zombie.ZombieSpawnRecorder.instance.record(isoZombie, this.getClass().getSimpleName());
                    }
                }
            }
        }
    }

    private IsoWindow getWeakestWindow(IsoPlayer isoPlayer) {
        IsoGridSquare isoGridSquare = null;
        Object object = null;
        IsoWindow isoWindow = null;
        int int0 = 0;

        for (int int1 = isoPlayer.getCurrentBuilding().getDef().getX() - 2; int1 < isoPlayer.getCurrentBuilding().getDef().getX2() + 2; int1++) {
            for (int int2 = isoPlayer.getCurrentBuilding().getDef().getY() - 2; int2 < isoPlayer.getCurrentBuilding().getDef().getY2() + 2; int2++) {
                isoGridSquare = IsoWorld.instance.getCell().getGridSquare(int1, int2, 0);
                if (isoGridSquare != null) {
                    object = isoGridSquare.getWindow();
                    if (object != null) {
                        int int3 = this.checkWindowStatus((IsoWindow)object);
                        if (int3 > int0) {
                            int0 = int3;
                            isoWindow = (IsoWindow)object;
                        }
                    }
                }
            }
        }

        return isoWindow;
    }

    public void wakeUp(IsoGameCharacter chr) {
        if (chr != null) {
            this.wakeUp(chr, false);
        }
    }

    public void wakeUp(IsoGameCharacter chr, boolean remote) {
        SleepingEventData sleepingEventData = chr.getOrCreateSleepingEventData();
        if (GameClient.bClient && !remote) {
            GameClient.instance.wakeUpPlayer((IsoPlayer)chr);
        }

        boolean _boolean = false;
        IsoPlayer isoPlayer = Type.tryCastTo(chr, IsoPlayer.class);
        if (isoPlayer != null && isoPlayer.isLocalPlayer()) {
            UIManager.setFadeBeforeUI(isoPlayer.getPlayerNum(), true);
            UIManager.FadeIn((double)isoPlayer.getPlayerNum(), 2.0);
            if (!GameClient.bClient && IsoPlayer.allPlayersAsleep()) {
                UIManager.getSpeedControls().SetCurrentGameSpeed(1);
                _boolean = true;
            }

            chr.setLastHourSleeped((int)isoPlayer.getHoursSurvived());
        }

        chr.setForceWakeUpTime(-1.0F);
        chr.setAsleep(false);
        if (_boolean) {
            try {
                zombie.GameWindow.save(true);
            } catch (Throwable throwable) {
                ExceptionLogger.logException(throwable);
            }
        }

        BodyPart bodyPart = chr.getBodyDamage().getBodyPart(BodyPartType.Neck);
        float _float = sleepingEventData.sleepingTime / 8.0F;
        if ("goodBed".equals(chr.getBedType())) {
            chr.getStats().setFatigue(chr.getStats().getFatigue() - Rand.Next(0.05F, 0.12F) * _float);
            if (chr.getStats().getFatigue() < 0.0F) {
                chr.getStats().setFatigue(0.0F);
            }
        } else if ("badBed".equals(chr.getBedType())) {
            chr.getStats().setFatigue(chr.getStats().getFatigue() + Rand.Next(0.1F, 0.2F) * _float);
            if (Rand.Next(5) == 0) {
                bodyPart.AddDamage(Rand.Next(5.0F, 15.0F));
                bodyPart.setAdditionalPain(bodyPart.getAdditionalPain() + Rand.Next(30.0F, 50.0F));
            }
        } else if ("floor".equals(chr.getBedType())) {
            chr.getStats().setFatigue(chr.getStats().getFatigue() + Rand.Next(0.15F, 0.25F) * _float);
            if (Rand.Next(5) == 0) {
                bodyPart.AddDamage(Rand.Next(10.0F, 20.0F));
                bodyPart.setAdditionalPain(bodyPart.getAdditionalPain() + Rand.Next(30.0F, 50.0F));
            }
        } else if (Rand.Next(10) == 0) {
            bodyPart.AddDamage(Rand.Next(3.0F, 12.0F));
            bodyPart.setAdditionalPain(bodyPart.getAdditionalPain() + Rand.Next(10.0F, 30.0F));
        }

        sleepingEventData.reset();
    }
}
